/**
 * 
 */
package jmotioncapture.geometry2D;

/**
 * A class extends {@link jmotioncapture.geometry2D.Point} and added the sorting
 * feature. The default sorting type is sorted by x-coordinates or it can
 * defined by <code>PointComparable.SORTING_BY_X</code> or
 * <code>PointComparable.SORTING_BY_Y</code>.
 * 
 * @author Kenneth
 * @since 1.0.0
 * 
 * @see <a
 *      href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html"
 *      target="_blank">java.lang (Interface Comparable)</a>
 */
@SuppressWarnings("unchecked")
public class PointComparable extends Point implements Comparable {

	/**
	 * Sorting by x-coordinate.
	 */
	public static final int SORTING_BY_X = 1;

	/**
	 * Sorting by y-coordinate.
	 */
	public static final int SORTING_BY_Y = 2;

	/**
	 * Sorting type. Default is set to <code>PointComparable.SORTING_BY_X</code>.
	 */
	int sortingType;

	/**
	 * Constructs a PointComparable with specified Point, sorting type is using
	 * x-coordinate.
	 * 
	 * @param pt
	 *            Point
	 */
	public PointComparable(Point pt) {
		super(pt.getX(), pt.getY(), pt.getID());
		this.setStaticCount(pt.getStaticCount());
		this.sortingType = SORTING_BY_X;
	}

	/**
	 * Constructs a PointComparable with specified Point and sorting type. The
	 * sorting type can be access by <code>PointComparable.SORTING_BY_X</code>
	 * or <code>PointComparable.SORTING_BY_Y</code>.
	 * 
	 * @param pt
	 *            Point
	 * @param sortingType
	 *            Sorting Type
	 * @see jmotioncapture.geometry2D.PointComparable#SORTING_BY_X
	 * @see jmotioncapture.geometry2D.PointComparable#SORTING_BY_Y
	 */
	public PointComparable(Point pt, int sortingType) {
		super(pt.getX(), pt.getY(), pt.getID());
		this.setStaticCount(pt.getStaticCount());
		this.sortingType = sortingType;
	}

	/**
	 * Comparsion method.
	 * 
	 * @return 0 if equal, 1 if num1 > num2, -1 if num1 < num2
	 */
	public int compareTo(Object o1) {

		if (sortingType == SORTING_BY_X) {
			return sort(this.getX(), ((Point) o1).getX());
		} else {
			return sort(this.getY(), ((Point) o1).getY());
		}
	}

	/**
	 * Comparsion method.
	 * 
	 * @param num1
	 *            first number
	 * @param num2
	 *            second number
	 * @return 0 if equal, 1 if num1 > num2, -1 if num1 < num2
	 */
	private int sort(int num1, int num2) {
		if (num1 == num2)
			return 0;
		else if (num1 > num2)
			return 1;
		else
			return -1;
	}
}
